package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    //1、条件分页查询用户列表
    @GetMapping
    public Result getUsers(UserDto userDto) {
        PageResult<User> userPage = userService.getUsers(userDto);
        log.info("查询用户列表，参数：{}", userDto);
        return Result.success(userPage);
    }
    //2、添加用户
    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("添加用户，参数：{}", user);
        userService.addUser(user);
        return Result.success();
    }
    //3、删除用户
    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable List<Integer> ids) {
        log.info("删除用户，参数：{}", ids);
        userService.deleteUser(ids);
        return Result.success();
    }
    //4、根据ID查询用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询用户：{}", id);
        return Result.success(userService.getById(id));

    }
    //5、修改用户
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("修改用户：{}", user);
        userService.update(user);
        return Result.success();
    }
    //6、根据角色查询用户
    @GetMapping("/role/{roleLabel}")
    public Result getByRole(@PathVariable String roleLabel) {
        log.info("根据角色查询用户：{}", roleLabel);
        return Result.success(userService.getByRoleLabel(roleLabel));
    }



}
